package com.acmedcare.framework.exchange.center.endpoint;

import com.acmedcare.framework.exchange.center.common.Page;
import com.acmedcare.framework.exchange.center.core.Const;
import com.acmedcare.framework.exchange.center.endpoint.exception.InvalidRequestParameterException;
import com.acmedcare.framework.exchange.center.endpoint.service.ConsumerService;
import com.acmedcare.framework.exchange.center.endpoint.service.PluginService;
import com.acmedcare.framework.exchange.center.entity.bean.Consumer;
import com.acmedcare.framework.exchange.center.entity.bean.Plugin;
import com.acmedcare.framework.exchange.center.entity.bean.PluginStatusEnum;
import com.acmedcare.framework.exchange.center.entity.dto.ConsumerCommand;
import com.acmedcare.framework.exchange.center.entity.dto.ConsumerDto;
import com.acmedcare.framework.exchange.center.entity.dto.ConsumerQueryDto;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "消费者配置器")
@RequestMapping("/consumers")
public class ConsumerEndpoint {

  final ConsumerService service;
  final RabbitTemplate rabbitTemplate;
  final ObjectMapper objectMapper;

  final PluginService pluginService;
  final PluginEndpoint pluginEndpoint;

  public ConsumerEndpoint(
      ConsumerService service,
      RabbitTemplate rabbitTemplate, ObjectMapper objectMapper,
      PluginService pluginService,
      PluginEndpoint pluginEndpoint) {
    this.service = service;
    this.rabbitTemplate = rabbitTemplate;
    this.objectMapper = objectMapper;
    this.pluginService = pluginService;
    this.pluginEndpoint = pluginEndpoint;
  }


  @ApiOperation("获取消费者信息")
  @GetMapping
  public ResponseEntity<Page<Consumer>> getConsumer(ConsumerQueryDto queryDto) {

    return ResponseEntity.ok(service.get(queryDto));
  }

  @ApiOperation("新增任务消费器")
  @PostMapping
  public ResponseEntity post(@RequestBody ConsumerDto consumerDto)
      throws InvalidRequestParameterException {

    valid(consumerDto);
    service.post(consumerDto);

    return ResponseEntity.ok().build();
  }

  @ApiOperation("修改任务消费器")
  @PutMapping("/{id}")
  public ResponseEntity put(@RequestBody ConsumerDto consumerDto,
      @PathVariable Long id)
      throws InvalidRequestParameterException {

    valid(consumerDto);
    service.put(consumerDto, id);

    return ResponseEntity.noContent().build();
  }

  @ApiOperation("删除任务消费器")
  @DeleteMapping("/{id}")
  public ResponseEntity delete(@PathVariable Long id) {

    service.delete(id);

    return ResponseEntity.ok().build();
  }

  @ApiOperation("消费者指令")
  @PostMapping("/command/{consumerId}/{status}")
  public ResponseEntity command(
      @PathVariable @ApiParam(value = "任务id", required = true) Long consumerId,
      @PathVariable @ApiParam(value = "任务状态", required = true) PluginStatusEnum status)
      throws Exception {

    ConsumerCommand consumerCommand = new ConsumerCommand();
    consumerCommand.setStatus(status);
    Consumer consumer = service.getById(consumerId);
    if (consumer == null) {

      throw new Exception("所查找的消费器不存在!");
    }

    consumerCommand.setConsumer(consumer);
    Plugin plugin = pluginService.getByName(consumer.getPluginName());
    if (plugin == null) {

      throw new InvalidRequestParameterException("插件不允许为空");
    }

    if (status.equals(PluginStatusEnum.LOADED)) {
      pluginEndpoint
          .command(plugin.getPluginId(), PluginStatusEnum.LOADED, consumer.getGroupName());
    }

    rabbitTemplate.setExchange(Const.ConsumerExchangeName);
    rabbitTemplate.convertAndSend(objectMapper.writeValueAsString(consumerCommand));
    consumer.setStatus(status);
    service.update(consumer);
    return ResponseEntity.ok().build();
  }


  private void valid(ConsumerDto consumerDto) throws InvalidRequestParameterException {

    if (StringUtils.isEmpty(consumerDto.getName())) {

      throw new InvalidRequestParameterException("消费者名称不允许为空！");
    }

    if (StringUtils.isEmpty(consumerDto.getConsumerName())) {

      throw new InvalidRequestParameterException("消费器名称不允许为空！");
    }

    if (StringUtils.isEmpty(consumerDto.getPluginName())) {

      throw new InvalidRequestParameterException("插件名称不允许为空！");
    }

    if (StringUtils.isEmpty(consumerDto.getTaskName())) {

      throw new InvalidRequestParameterException("任务名称不允许为空！");
    }

    if (StringUtils.isEmpty(consumerDto.getVersion())) {

      throw new InvalidRequestParameterException("版本号不允许为空！");
    }

    if (StringUtils.isEmpty(consumerDto.getGroupName())) {

      throw new InvalidRequestParameterException("消费器所属的环境不允许为空！");
    }
  }
}
